from datetime import datetime

from common.tdengine import load_tdengine_pool
from iot.models import Warn

host = '39.98.122.153'
database = 'iot'
password = 'taosdata'
user = 'root'


def get_point_data(name, point_code, start, end):
    data = {}
    with load_tdengine_pool().get() as cursor:
        sql = f"select * from {point_code} where ts>'{start}' and ts<'{end}';"
        cursor.execute(sql)
        rows = cursor.fetchall()
        value = []
        time = []
        for row in rows:
            value.append(round(row[1], 2))
            time.append(datetime.strftime(row[0], '%Y-%m-%d %H:%M:%S'))
        data = {'name': name, 'time': time, 'value': value}
    return data


def get_warn_and_early_warn(queryset, point_type, page, limit):
    if not page:
        page = str(1)
    if not limit:
        limit = str(8)
    page = int(page)
    limit = int(limit)
    warns = queryset.all()
    print(
        (page - 1) * limit
    )
    print(page * limit)
    warn = queryset.filter(point__unit__type=point_type).order_by('is_handel')[(page-1)*limit: page*limit]
    point_data = [
        {
            'id': each.id,
            'dept': each.point.cabinet.dept.name,
            'cabinet': each.point.cabinet.name,
            'point': each.point.name,
            'value': each.value,
            'is_handel': each.is_handel,
            'time': datetime.strftime(each.create_datetime, '%Y-%m-%d %H:%M:%S'),
            'type': '预警' if each.type==0 else '报警'
        }
        for each in warn
    ]
    data = {'total': len(warns), 'point_data': point_data}
    return data


def get_warn(queryset, point_type, warn_type, page, limit, start, end):
    start = start + ' 00:00:00'
    end = end + ' 23:59:59'
    if not page:
        page = str(1)
    if not limit:
        limit = str(8)
    page = int(page)
    limit = int(limit)
    warns = queryset.filter(type=warn_type).filter(create_datetime__gte=start).filter(create_datetime__lte=end).\
            filter(point__unit__type=point_type)
    print(
        (page - 1) * limit
    )
    print(page * limit)
    warn = warns.order_by('is_handel')[(page-1)*limit: page*limit]
    point_data = [
        {
            'id': each.id,
            'dept': each.point.cabinet.dept.name,
            'cabinet': each.point.cabinet.name,
            'point': each.point.name,
            'value': each.value,
            'is_handel': each.is_handel,
            'time': datetime.strftime(each.create_datetime, '%Y-%m-%d %H:%M:%S')
        }
        for each in warn
    ]
    data = {'total': len(warns.all()), 'point_data': point_data}
    return data


def applet_warning(point_id):
    point_warn = Warn.objects.filter(point__id=point_id).order_by('-create_datetime')[0]
    data = {
        'id': point_warn.id,
        'cabinet': point_warn.point.cabinet.name,
        'cabinetId': point_warn.point.cabinet.id,
        'point': point_warn.point.name,
        'value': point_warn.value,
        'is_handel': point_warn.is_handel,
        'time': datetime.strftime(point_warn.create_datetime, '%Y-%m-%d %H:%M:%S')
    }
    return data
